package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.util.NumberUtils;
import com.cskaoyan.order.bean.bo.InitOrderBo;
import com.cskaoyan.order.biz.callback.SendEmailCallback;
import com.cskaoyan.order.biz.callback.TransCallback;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.constant.OrderConstants;
import com.cskaoyan.order.constant.OrderItemConstants;
import com.cskaoyan.order.constant.OrderRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dto.CartProductDto;
import com.cskaoyan.order.utils.GlobalIdGeneratorUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 初始化订单 生成订单
 */

@Slf4j
@Component
public class InitOrderHandler extends AbstractTransHandler {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderConverter orderConverter;

    @Override
    public boolean isAsync() {
        return false;
    }

    @SneakyThrows
    @Override
    public boolean handle(TransHandlerContext context) {
        CreateOrderContext orderContext = (CreateOrderContext) context;
        // order表
        // 获取唯一订单id未有 --- 工具类/ userId已有、username已有/ unique_key已有

        // 插入status 0 / create_time / update_time / user_id/ unique_key/ buyer_nick
        String orderId = new GlobalIdGeneratorUtil().getNextSeq("orderId", 1);
        InitOrderBo initOrderBo = new InitOrderBo(OrderConstants.ORDER_STATUS_INIT,new Date(),new Date());
        Order order = orderConverter.orderContextToOrder(orderContext,initOrderBo);
        order.setOrderId(orderId);
        order.setBuyerNick(orderContext.getUserName());
        order.setPayment(orderContext.getOrderTotal());
        orderMapper.insertSelective(order);

        // orderItem表
        // 获取唯一订单商品id --- 工具类
        // 获取orderId
        // 备注salePrice是单价price/orderTotal是total_fee
        // String orderItemId = new GlobalIdGeneratorUtil().getNextSeq("orderItemId", 1);
        // 根据List<xxxBO>插入数据
        // 插入商品item_id/order_id/num/title/price/total_fee/pic_path/status 1/ create_time/update_time
        ArrayList<Long> byProductIds = new ArrayList<>();
        List<CartProductDto> cartProductDtoList = orderContext.getCartProductDtoList();
        for (CartProductDto cartProductDto : cartProductDtoList) {
            OrderItem orderItem = new OrderItem();
            String orderItemId = new GlobalIdGeneratorUtil().getNextSeq("orderItemId", 1);
            orderItem.setId(orderItemId);
            orderItem.setItemId(cartProductDto.getProductId());
            orderItem.setOrderId(orderId);
            orderItem.setNum(cartProductDto.getProductNum().intValue());
            orderItem.setPrice(cartProductDto.getSalePrice().doubleValue());
            orderItem.setTitle(cartProductDto.getProductName());
            orderItem.setPicPath(cartProductDto.getProductImg());
            orderItem.setTotalFee(cartProductDto.getSalePrice().multiply(new BigDecimal(cartProductDto.getProductNum())).doubleValue());
            orderItem.setStatus(OrderItemConstants.ORDERITEM_STATUS_LOCKED);
            byProductIds.add(cartProductDto.getProductId());
            orderItemMapper.insert(orderItem);
        }

        orderContext.setOrderId(orderId);
        orderContext.setBuyProductIds(byProductIds);



        return true;
    }

}
